<template>
  <div class="employees-detail-container">
    <div class="app-container">
      <el-card>
        <a href="#" @click="$router.back()">返回</a>
        <!-- 放置表单 -->
        <el-form
          ref="userForm"
          label-width="120px"
          style="margin-left: 120px; margin-top: 30px"
          :model="userInfo"
          :rules="rules"
        >
          <el-form-item label="姓名:" prop="username">
            <el-input v-model="userInfo.username" style="width: 300px" />
          </el-form-item>
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" style="width: 300px" disabled />
          </el-form-item>
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
            />
          </el-form-item>
          <el-form-item label="员工头像">
            <ImageUpdate ref="addSatsffPhoto" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addfnUpdate">更新</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserAPI } from '@/api/user'
import { AddUpdataUSerInfoAPI } from '@/api/emploees'
export default {
  name: 'EmployeesDetail',
  data() {
    return {
      userInfo: {
        username: '',
        mobile: '',
        staffPhoto: '',
        timeOfEntry: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不可为空', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不可为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    staffForm() {
      return this.$refs.addSatsffPhoto.fileList.every(item => item.status === 'success')
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    async getUserInfo() {
      const res = await getUserAPI(this.$route.params.id)
      console.log(res)
      this.userInfo = res.data
      this.$refs.addSatsffPhoto.fileList = [{ url: res.data.staffPhoto }]
    },
    addfnUpdate() {
      this.$refs.userForm.validate(async(flag) => {
        if (flag) {
          const staffPhoto = this.$refs.addSatsffPhoto
          const url = staffPhoto.fileList[0]?.url
          if (!url) {
            this.$message.warning('图片不能为空')
            return
          }
          if (!this.staffForm) {
            this.$message.warning('图片正在上传，请稍后继续')
            return
          }
          this.userInfo.staffPhoto = url
          const res = await AddUpdataUSerInfoAPI(this.userInfo)
          console.log(res)
          this.$message.success('更新成功')
        }
      })
    }
  }
}
</script>
